package com.edais.util.fuyou;

import com.edais.util.CommonUtil;
import com.edais.util.JsonUtil;
import com.edais.util.RSAThirdParty.RSA;
import com.edais.util.ThirdPartyEnvUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.net.URLEncoder;
import java.util.Map;

public class FuyouThridPartyFunctionUtil {
	private static final Log logger = LogFactory.getLog(FuyouThridPartyFunctionUtil.class);
	
	/**
	 * 连连支付-查询银行卡卡bin信息
	 * @param paramMap
	 * @return
	 */
	public static Map<String,String> bankCardQuery(Map<String, String> paramMap){
		String url = ThirdPartyEnvUtil.getValue("bankCardQuery");

		String requestParam = "keyRandom=KEY_RANDOM";
		String publicKeyParam="";
		
		try {
			publicKeyParam = RSA.getPublick_Key_Encrypt(paramMap);
			requestParam = requestParam.replace("KEY_RANDOM", URLEncoder.encode(publicKeyParam, "utf-8"));
		} catch (Exception e) {
			logger.debug("公钥加密串："+e);
		}
		logger.debug("查询银行卡卡bin信息请求参数："+requestParam);
		
		String bankCardQuery =  CommonUtil.sendPostByHttp(url, requestParam);
		
		if( null == bankCardQuery ){
			logger.error("查询银行卡卡bin信息信息失败");
			return null;
		}else{
			//解析发送结果
			Map<String,String> bankCardQueryMap = JsonUtil.getMapFromJsonString(bankCardQuery);
			return bankCardQueryMap;
		}
	}
	
	/**
	 * 查询用户签约信息
	 * @param paramMap
	 * @return
	 */
	public static Map<String,String> userBankCard(Map<String, String> paramMap){
		String url = ThirdPartyEnvUtil.getValue("userBankCard");
		String requestParam = "keyRandom=KEY_RANDOM";
		String publicKeyParam="";
		
		try {
			publicKeyParam = RSA.getPublick_Key_Encrypt(paramMap);
			requestParam = requestParam.replace("KEY_RANDOM", URLEncoder.encode(publicKeyParam, "utf-8"));
		} catch (Exception e) {
			logger.debug("公钥加密串："+e);
		}
		logger.debug("查询用户签约信息请求参数："+requestParam);
		
		String userBankCard =  CommonUtil.sendPostByHttp(url, requestParam);
		
		if( null == userBankCard ){
			logger.error("查询用户签约信息失败");
			return null;
		}else{
			//解析发送结果
			Map<String,String> userBankCardMap = JsonUtil.getMapFromJsonString(userBankCard);
			return userBankCardMap;
		}
	}
	/**
	 * 连连支付-实名认证
	 * @param paramMap
	 * @return
	 */
	public static Map<String,String> getAuth(Map<String,String> paramMap){
		String url = ThirdPartyEnvUtil.getValue("getAuth");
		
		String requestParam = "keyRandom=KEY_RANDOM";
		String publicKeyParam="";
		
		try {
			publicKeyParam = RSA.getPublick_Key_Encrypt(paramMap);
			requestParam = requestParam.replace("KEY_RANDOM", URLEncoder.encode(publicKeyParam, "utf-8"));
		} catch (Exception e) {
			logger.debug("公钥加密串："+e);
		}
		logger.debug("实名认证信息请求参数："+requestParam);
		
		String auth = CommonUtil.sendPostByHttp(url, requestParam);
		
		if(null == auth){
			logger.error("实名认证信息失败");
			return null;
		}else{
			//解析发送结果
			Map<String,String> authMap = JsonUtil.getMapFromJsonString(auth);
			return authMap;
		}
	}
	/**
	 * 连连支付-查询支付结果
	 * @param paramMap
	 * @return
	 */
	public static Map<String,String> orderQuery(Map<String,String> paramMap){
		String url = ThirdPartyEnvUtil.getValue("orderQuery");
		String requestParam = "keyRandom=KEY_RANDOM";
		String publicKeyParam="";
		
		try {
			publicKeyParam = RSA.getPublick_Key_Encrypt(paramMap);
			requestParam = requestParam.replace("KEY_RANDOM", URLEncoder.encode(publicKeyParam, "utf-8"));
		} catch (Exception e) {
			logger.debug("公钥加密串："+e);
		}
		logger.debug("查询支付结果请求参数："+requestParam);
		
		String orderQuery = CommonUtil.sendPostByHttp(url, requestParam);
		
		if(null == orderQuery){
			logger.error("查询支付结果失败");
			return null;
		}else{
			//解析发送结果
			Map<String,String> orderQueryMap = JsonUtil.getMapFromJsonString(orderQuery);
			return orderQueryMap;
		}
	}
	
	
	public static Map<String,String> bankCardUnbind(Map<String,String> paramMap){
		String url = ThirdPartyEnvUtil.getValue("bankCardUnbind");
		String requestParam = "keyRandom=KEY_RANDOM";
		String publicKeyParam="";
		
		try {
			publicKeyParam = RSA.getPublick_Key_Encrypt(paramMap);
			requestParam = requestParam.replace("KEY_RANDOM", URLEncoder.encode(publicKeyParam, "utf-8"));
		} catch (Exception e) {
			logger.debug("公钥加密串："+e);
		}
		logger.debug("解绑银行卡结果请求参数："+requestParam);
		
		String bankCardUnbind = CommonUtil.sendPostByHttp(url, requestParam);
		
		if(null == bankCardUnbind){
			logger.error("解绑银行卡结果失败");
			return null;
		}else{
			//解析发送结果
			Map<String,String> bankCardUnbindMap = JsonUtil.getMapFromJsonString(bankCardUnbind);
			return bankCardUnbindMap;
		}
	}
}
